<?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
//A date in the past
echo "<?xml version='1.0' encoding='UTF-8'?>";

$con = mysql_connect("localhost","cubeMass_f","cubeSelectNow");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
// Create table in my_db database
mysql_select_db("cubeMass", $con);
session_start();

if($_POST["state"]=="application"){
	$result =  mysql_query("SELECT * FROM user WHERE name = '$_POST[userName]'");
	$row = mysql_fetch_array($result);
	if($row['password']){
		echo "<application>";
		echo "<info>theSame</info>";
		echo "</application>";
		}
		else{
			$password = MD5($_POST['password']);
			$createTime = date("Y-m-d H:i:s");
			$ip=$_SERVER['REMOTE_ADDR'];
			
			
			
			$sql = "INSERT INTO user (name,password,createTime,lastLoginTime,loginIp,lastLoginIp,loginTimes,email) 
							VALUES (	
									'$_POST[userName]',
									'$password',
									'$createTime',
									'$createTime',
									'$ip',
									'$ip',
									'1',
									'$_POST[email]'
									)";
			mysql_query($sql,$con);
			//读取一次
			$result =  mysql_query("SELECT * FROM user WHERE name = '$_POST[userName]'");
			$row = mysql_fetch_array($result);
			$_SESSION["user_id"] = $row['ID'];
			$_SESSION["name"] = $row['name'];
			if($row['password']){
				echo "<application>";
				echo "<info>ok</info>";
				echo "<id>$_SESSION[user_id]</id>";
				echo "<name>$_SESSION[name]</name>";
				echo "</application>";
				}
		}
	}
elseif($_POST["state"]=="checkName"){
	$result =  mysql_query("SELECT * FROM user WHERE name = '$_POST[userName]'");
	if($row = mysql_fetch_array($result)){
		echo "<checkName>";
		echo "<info>theSame</info>";
		echo "</checkName>";
		}
		else{
			echo "<checkName>";
			echo "<info>available</info>";
			echo "</checkName>";
			}
	}

mysql_close($con);
?>